import rclpy
from rclpy.node import Node
from std_msgs.msg import String, UInt32

class WriterNode(Node):
    def __init__(self, name):
        super().__init__(name)
        self.get_logger().info("Hello, I am writer %s." % name)
        self.pub_novel = self.create_publisher(String, "book", 10)

        self.timer_period = 5
        self.count = 0
        self.timer = self.create_timer(self.timer_period,self.timer_callback)

        self.account = 80
        self.sub_money = self.create_subscription(UInt32, "money", self.recv_money_callback, 10)

    def timer_callback(self):
        msg = String()
        msg.data = "The %d chapter: xx %d times meet xx" % (self.count, self.count)
        self.pub_novel.publish(msg)
        self.get_logger().info("Publish a chapter novel, the text is : %s" % msg.data)
        self.count+=1

    def recv_money_callback(self, money):
        self.account += money.data
        self.get_logger().info("receive %d writer salary, now there are %d dollars in account." % (money.data, self.account))



def main(args=None):
    rclpy.init(args=args)
    li4_node = WriterNode("li4")
    # node = Node("li4")
    # node.get_logger().info("hi, I am li4.")
    rclpy.spin(li4_node)
    rclpy.shutdown()